package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.HospitalException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.sun.scenario.effect.impl.prism.PrMergePeer;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.PanelUI;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    //医院设置的service
    @Autowired
    HospitalSetService hospitalSetService;

    //科室操作
    @Autowired
    private DepartmentService departmentService;
    //排班
    @Autowired
    private ScheduleService scheduleService;

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院传递来的签名,进行加密
        String sign = (String)paramMap.get("sign");
        //通过hoscode获取编号查询医院设置表中的签名是否相同
        String hoscod = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscod);
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        //查出的签名进行加密
        String s = MD5.encrypt(signKey);
         if(!sign.equals(s)){
             throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
         }
        log.info("保存的数据{}",paramMap);
        hospitalService.save(paramMap);
        return Result.ok();
    }
    //医院信息
    @RequestMapping("hospital/show")
    public Result findhosp(HttpServletRequest request){
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取code进行查询
        String hoscode = (String)paramMap.get("hoscode");
        String sign = (String)paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll("", "+");
            paramMap.put("logoData", logoData);
        }
        //进行校对签名
        String s = MD5.encrypt(signKey);
        log.info("医院传递的sign{},查询校对后的sign{}",sign,s);
        if(!sign.equals(s)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        log.info("医院信息 {}",hoscode);
        //查出的签名进行加密
        Hospital hospital=hospitalService.getfind(hoscode);
        return Result.ok(hospital);
    }
    //上传科室的接口
    @RequestMapping("/saveDepartment")
    public Result departmentout(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院code进行查询
        String hoscode = (String)paramMap.get("hoscode");
        String sign = (String)paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //进行校对签名
        String s = MD5.encrypt(signKey);
        log.info("医院传递的sign{},查询校对后的sign{}",sign,s);
        if(!sign.equals(s)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用dept科室service的方法
        departmentService.save(paramMap);
      return Result.ok();
    }
    //查询科室的接口
    @RequestMapping("department/list")
    public Result finddept(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页 和 每页记录数 如果为null默认1
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        //通过传入的页数和大小，code进行查询

        //医院和科室的抽取的实体类
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(pageModel);
    }
    //删除科室
    @RequestMapping("department/remove")

    public Result rmovedept(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号 和 科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();

    }
    //上传排班信息
    @RequestMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        scheduleService.save(paramMap);
        return Result.ok();

    }
    //查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递的排班信息 把信息通过redirectAttributes保存在参数域中 通过getParameterMap() 取出来
        Map<String, String[]> parameterMap = request.getParameterMap();
        //使用工具类进行遍历转化
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //把信息都放在了map中可以通过get() 取出参数
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String depcode = (String)paramMap.get("depcode");
        ////当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));

//        Integer page =(Integer) paramMap.get("page");
//        if(StringUtils.isEmpty(page)){
//             page=1;
//        }else {
//            Integer.parseInt((String) paramMap.get("page"));
//        }

        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        //医院和科室的抽取的实体类
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }
    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号和排班编号
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        //TODO 签名校验

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

}
